﻿#region HeaderComments
// ***********************************************************************************************************************************************
// Copyright (C) # YEAR # # York #
// 作    者：# CaiTao #
// 创建日期：# CREATIONDATE #
// 功能描述：# 动画组组件，允许同时播放多个动画 #
// 修改记录：# MODIFICATIONRECORD #
// ***********************************************************************************************************************************************
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace AnimationComponent
{
    public class AnimationGroup :MonoBehaviour, IPlay,IPlayAsync
    {
        public List<IAnimatiomn> animations = new List<IAnimatiomn>();

        public void PlayBackwards()
        {
            for (int i = 0; i < animations.Count; i++)
            {
                animations[i].PlayBackwards();
            }
        }
        public void PlayForward() 
        {
            for (int i = 0; i < animations.Count; i++)
            {
                animations[i].PlayForward();
            }
        }

        /// <summary>
        /// 动画播放
        /// </summary>
        /// <param name="State"></param>
        public void PlayAnimation()
        {
            for (int i = 0; i < animations.Count; i++)
            {
                animations[i].PlayAnimation();
            }
        }

        private void OnDestroy()
        {
            animations.Clear();
        }

        public IEnumerator PlayAnimationAsync()
        {
            bool[] states = new bool[animations.Count];
            for (int i = 0; i < animations.Count; i++)
            {
                states[i] = false;
                StartCoroutine(PlayAnimationAsync_C(animations[i],i,(index,state)=> states[index] = state));
            }

            yield return new WaitUntil(() => states.All((state) => state));
        }

        private IEnumerator PlayAnimationAsync_C(IAnimatiomn animatiomn,int index, Action<int,bool> isComplete)
        {
            yield return animatiomn.PlayAnimationAsync();
            isComplete?.Invoke(index, true);
        }

        public IEnumerator PlayBackwardsAsync()
        {
            bool[] states = new bool[animations.Count];
            for (int i = 0; i < animations.Count; i++)
            {
                states[i] = false;
                StartCoroutine(PlayBackwardsAsync_C(animations[i], i, (index, state) => states[index] = state));
            }

            yield return new WaitUntil(() => states.All((state) => state));
        }

        private IEnumerator PlayBackwardsAsync_C(IAnimatiomn animatiomn, int index, Action<int, bool> isComplete)
        {
            yield return animatiomn.PlayBackwardsAsync();
            isComplete?.Invoke(index, true);
        }

        public IEnumerator PlayForwardAsync()
        {
            bool[] states = new bool[animations.Count];
            for (int i = 0; i < animations.Count; i++)
            {
                states[i] = false;
                StartCoroutine(PlayForwardAsync_C(animations[i], i, (index, state) => states[index] = state));
            }

            yield return new WaitUntil(()=> states.All((state) => state));
        }

        private IEnumerator PlayForwardAsync_C(IAnimatiomn animatiomn, int index, Action<int, bool> isComplete)
        {
            yield return animatiomn.PlayForwardAsync();
            isComplete?.Invoke(index, true);
        }
    }
}

